<?php
/**
* Класс-контроллер регистрации пользователей
* 
* @author Larein
* @version 1.0.0 / 28.07.2011
*/
class RegistrationController extends FrontEndController
{
    public function init() 
    {
        Y::redirAuthed('user/');    
    }
        
    public function actions()
    {        
        return array(
            'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=> 0xE8F4FF,
                'foreColor'=>0x5680A8,
                'maxLength'=> 7,
                'minLength'=> 7, 
                'padding' =>3,
                'width' => 220,
                'height'=> 80,                               
            ),
        );
    }
    
    public function actionIndex()
    {
         $model = new RegistrationForm(); 
         
         if(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')
         {
            echo CActiveForm::validate($model);
            Yii::app()->end();
         }
        
         if(isset($_POST['RegistrationForm']))
         {
             $model->attributes = $_POST['RegistrationForm']; 
             if($model->validate())
             {
                 $model->password = sha1(sha1($model->password));
                 $model->actcode = $model->randomString(30);
                 $model->create_time = time();
                 
                 if($model->insert(array('name', 'password', 'squestion', 'answer', 'email', 'actcode', 'create_time')))
                 {
                     $model->sendRegistrationEmail();
                     $this->render('registration_success');   
                     Yii::app()->end();
                 }
             }
         }       
        
        $script= '
            $("#terms_link").click(function(){
                $("#terms").slideToggle(300);
            });        
        ';
        
        Yii::app()->clientScript->registerCoreScript('jquery');
        Yii::app()->clientScript->registerScript('terms', $script, CClientScript::POS_READY);
        
        $this->render('index', array('model'=>$model));
    }
    
    public function actionActivation($email=null, $key=null)
    {
       $model = new RegistrationForm();
       $model->activateAccount($email, $key);
       $this->render('activation', array('model'=>$model));       
    }
}